package cn.sanshuilin.kst.system.provider.serviceimpl;

import cn.sanshuilin.kst.common.utils.FastJsonUtils;
import cn.sanshuilin.kst.common.utils.IdGenerator;
import cn.sanshuilin.kst.system.api.service.IBrokeService;
import cn.sanshuilin.kst.system.provider.bean.Location;
import cn.sanshuilin.kst.system.provider.bean.LoginRecordBean;
import cn.sanshuilin.kst.system.provider.bean.SubjectBean;
import cn.sanshuilin.kst.system.provider.write.BookJPA;
import cn.sanshuilin.kst.system.provider.write.dao.LocationDao;
import cn.sanshuilin.kst.system.provider.write.dao.LoginRecordDao;
import cn.sanshuilin.kst.system.provider.write.dao.SubjectDao;
import cn.sanshuilin.kst.system.provider.write.dao.SubjectReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2018-03-02 14:16
 * Copyright 2018  by 三水林科技有限公司
 **/
@Service("brokeService")
public class BrokeService implements IBrokeService {
    @Autowired
    private BookJPA bookJPA;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private LoginRecordDao loginRecordDao;
    @Autowired
    private LocationDao locationDao;



    @Override
    public void saveBroke(String token, List imageAttachmentIDs, String lng, String lat, String address, String description)
    {
        LoginRecordBean loginRecordBean = loginRecordDao.findByToken(token);
        String accountId = loginRecordBean.getAccountId();

        Location location = locationDao.findById(accountId);
        if (null==location){
            Location location1 = new Location();
            location1.setId(accountId);
            location1.setAddress(address);
            location1.setLat(Double.parseDouble(lat));
            location1.setLng(Double.parseDouble(lng));
            locationDao.saveAndFlush(location1);
        }else {
            locationDao.updateLocation(accountId,address,Double.parseDouble(lat),Double.parseDouble(lng));
        }

        SubjectBean subject = new SubjectBean();
        subject.setId(IdGenerator.INSTANCE.nextId());
        subject.setPubMemberId(accountId);//发布话题的用户ID
        subject.setImageAttachmentIDs(FastJsonUtils.toJSONString(imageAttachmentIDs));
        subject.setDescription(description);
        SubjectBean subject1 = subjectDao.saveAndFlush(subject);

    }
}
